<?php

namespace app\api\controller\web;

use app\admin\model\apply\Confg;
use app\admin\model\nursery\Attestation;
use app\admin\model\nursery\Companyattestation;
use app\common\controller\Api;
use app\common\model\Attachment;
use app\common\model\User;
use think\Cache;
use think\Db;
use think\Model;
use think\Validate;

/**
 *  公共接口
 */
class Common extends Api
{
    protected $noNeedLogin = ['userAuth','userTopList','cityList'];
    protected $noNeedRight = '*';
    protected $model ='';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 个人实名认证提交
     */
    public function userAuth()
    {
        $user = $this->auth->getUser();
        $name = $this->request->param('name');
        $mobile = $this->request->param('phone');
        $card = $this->request->param('card');
        if (!$name || !$mobile || !$card) {
            $this->error('参数不全');
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('电话号码格式不正确');
        }
        if (!Validate::regex($card, "/^\d{17}[0-9xX]$/")) {
            $this->error('身份证格式不正确');
        }
        $Attestation = (new Attestation())->where(['user_id'=>$user['id']])->order('create_time desc')->find();
        if($Attestation){
            if($Attestation['status'] == '0'){
                $this->error('正在审核中,请耐心等待');
            }
            if($Attestation['status'] == '1' ){
                $this->error('已认证');
            }
        }
        $data=[
            'user_id'=>$user['id'],
            'name'=>$name,
            'phone'=>$mobile,
            'card'=>$card,
            'status'=>0,
            'create_time'=>date('Y-m-d H:i:s'),
        ];
        $user = Attestation::create($data);
        if($user){
            $this->success("提交成功");
        }
        $this->error("提交失败");
    }


    /**
     * 苗圃（公司）认证提交
     */
    public function companyAuth()
    {
        $user = $this->auth->getUser();
        $company_name = $this->request->param('company_name');
        $mobile = $this->request->param('phone');
        $gacade_img = $this->request->param('gacade_img');
        $business_img = $this->request->param('business_img');
        $area = $this->request->param('area');
        $introduction = $this->request->param('introduction');
        if (!$company_name || !$gacade_img || !$business_img || !$mobile || !$area || !$introduction) {
            $this->error('参数不正确');
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机格式不正确');
        }
        $data=[
            'company_name'=>$company_name,
            'gacade_img'=>$gacade_img,
            'business_img'=>$business_img,
            'phone'=>$mobile,
            'area'=>$area,
            'status'=>0,
            'introduction'=>$introduction,
            'audit_time'=>null,
        ];
        $reulst = Companyattestation::get(['user_id'=>$user['id']]);

        $Confg = \app\admin\model\apply\Confg::get(1);
        if($Confg['nursery_certificat'] <= 0){  //当苗圃认证费用为0 默认已缴费
            $data['is_pay'] = 1;
        }

        if($reulst){
            $result = Companyattestation::update($data,['user_id'=>$user['id']]);
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['user_id'] = $user['id'];
            $result = Companyattestation::create($data);
         }
        $info = Companyattestation::get(['user_id'=>$user['id']]);
        if($result){
            $this->success("提交成功",['ca_id'=>$info['id']]);
        }
        $this->error("提交失败");
    }

    /**
     * 城市列表 三级
     */
    public function cityList()
    {
        $list = Cache::get('cityList');
        if(!$list){
            $lvl =  Db::name('area')->field('id,pid,name')->where(['level'=>1])->select();
            if($lvl){
                $lvl2 =  Db::name('area')->field('id,pid,name')->where(['level'=>2])->select();
                $lvl3 =  Db::name('area')->field('id,pid,name')->where(['level'=>3])->select();
                foreach ($lvl as &$v){
                    foreach ($lvl2 as &$v2){
                        if($v['id'] == $v2['pid']){
                            foreach ($lvl3 as $v3){
                                if($v2['id'] == $v3['pid']){
                                    $v2['childs'][]= $v3;
                                }
                            }
                            $v['childs'][]= $v2;
                        }
                    }
                }
            }
            Cache::set('cityList',$lvl,3600);
            $list = Cache::get('cityList');
        }

        $this->success("获取成功",$list);;
    }


    /**
     * 城市列表 二级
     */
    public function cityList2()
    {
        $list = Cache::get('cityList3');

        if(!$list){
            $lvl =  Db::name('area')->field('id,pid,name')->where(['level'=>1])->select();
            if($lvl){
                $lvl2 =  Db::name('area')->field('id,pid,name')->where(['level'=>2])->select();
                foreach ($lvl as &$v){
                    foreach ($lvl2 as &$v2){
                        if($v['id'] == $v2['pid']){
                            $v['childs'][]= $v2;
                        }
                    }
                }
            }
            Cache::set('cityList3',$lvl,3600);
            $list = Cache::get('cityList3');
        }

        $this->success("获取成功",$list);;
    }


}
